package com.rs.service.impl.rs;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.ProjectMapper;
import com.rs.model.CommonResult;
import com.rs.model.User;
import com.rs.model.Project;
import com.rs.model.Relation;
import com.rs.service.rs.ProjectService;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class ProjectServiceImpl implements ProjectService {
	@Autowired
	private ProjectMapper pm;

	/**********APP***********/

	/**
	 * 根据职务类别查询员工
	 */
	@Override
	public CommonResult<List<User>> projectByPositionLoUserApp(String posType) {
		List<User> list = pm.projectByPositionLoUserApp(posType);
		return new CommonResult<List<User>>(200, "查询成功", list);
	}

	/**
	 * 建立项目
	 */
	@Transactional
	@Override
	public CommonResult<String> projectEstablishApp(Project project) {
		project.setPr_ktime(new Date());//给项目赋值当前时间
		project.setPr_post(0);//默认项目为立项
		int count = pm.projectEstablishApp(project);//新建项目
		if (count != 0) {
			//添加项目人员
			Relation[] relations = project.getRelations();
			//循环添加relation
			for (Relation relation : relations) {
				relation.setRel_prid(project.getPr_id());//将项目id赋值对象
				relation.setRel_prname(project.getPr_name());//将项目名称赋值对象
				pm.relationAdd(relation);
				pm.ProgressAdd(relation.getRel_id());//同时添加进度表
			}
			return new CommonResult<String>(200, "新建成功");
		} else {

			return new CommonResult<String>(200, "新建失败");
		}
	}

	/**
	 * 查看所有的立项申请(条件查询)
	 *
	 */
	@Override
	public CommonResult<List<Project>> projectLookAllApp(Project project) {

		List<Project> list = pm.projectLookAllApp(project);

		return new CommonResult<List<Project>>(200, "查询成功", list);
	}

	/**
	 * 根据项目建立时间查询
	 * @throws ParseException 
	 */
	@Override
	public CommonResult<List<Project>> projectAccordanceTimeLookApp(Map<String, String> map) {

		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));//开始时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));//结束时间

		List<Project> list = pm.projectAccordanceTimeLookApp(startTime, endTime);
		return new CommonResult<List<Project>>(200, "查询成功", list);
	}

	/**
	 * 查看项目详情
	 */
	@Override
	public CommonResult<Project> projectLookParticularApp(Project project) {
		Project p = pm.projectLookParticularApp(project.getPr_id());
		return new CommonResult<Project>(200, "查询成功", p);
	}

	/**
	 * 修改项目记录
	 */
	@Transactional
	@Override
	public CommonResult<String> projectUpdateProjectApp(Project project) {
		//判断修改人是否为立项人
		boolean flag = Optional.ofNullable(pm.selectPruid(project)).isPresent();
		if (flag) {//修改人为负责人

			int count = pm.projectUpdateProjectApp(project);//修改记录
			if (count != 0) {

				return new CommonResult<String>(200, "修改成功");
			} else {
				return new CommonResult<String>(200, "修改失败");
			}

		} else {//修改人不是负责人

			return new CommonResult<String>(200, "项目记录只能由负责人修改");
		}
	}

	/**
	 * pc端条件查询所有的项目记录
	 */
	@Override
	public CommonResult<Map<String, Object>> projectConditionsQueryPc(Project project) {
		Page<Project> page = PageHelper.startPage(project.getPageNum(), 10);//分页
		List<Project> list = pm.projectConditionsQueryPc(project);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());
		map.put("pages", page.getPages());
		map.put("pageNum", page.getPageNum());
		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * pc端根据建立时间查询项目记录
	 */
	@Override
	public CommonResult<Map<String, Object>> projectAccordanceTimeQueryPC(Map<String, String> map) {
		Integer pageNum = Integer.valueOf(map.get("pageNum"));//当前页

		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));//开始时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));//结束时间

		Page<Project> page = PageHelper.startPage(pageNum, 10);//分页
		List<Project> list = pm.projectAccordanceTimeQueryPC(startTime, endTime);

		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc端查询项目详情
	 */
	@Override
	public CommonResult<Project> projectQueryOnePc(Integer pr_id) {
		Project p = pm.projectQueryOnePc(pr_id);
		return new CommonResult<Project>(200, "查询成功", p);
	}

}
